package com.tangye.massageshop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.tangye.massageshop.constants.SysConstant;
import com.tangye.massageshop.exception.ShopException;
import com.tangye.massageshop.repository.UserRepository;
import com.tangye.massageshop.entity.CompanyEntity;
import com.tangye.massageshop.entity.UserEntity;
import com.tangye.massageshop.mapper.CompanyMapper;
import com.tangye.massageshop.mapper.UserMapper;
import com.tangye.massageshop.service.UserService;
import com.tangye.massageshop.util.MD5Utils;
import com.tangye.massageshop.util.ThreadLocalUtil;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;
    @Resource
    private UserRepository userRepository;
    @Resource
    private CompanyMapper companyMapper;


    @Override
    public List<UserEntity> getAll() {
        return userMapper.selectList(new QueryWrapper<>());
    }

    @Override
    public List<UserEntity> queryUserListPage(UserEntity userEntity) {
        return userRepository.selectUsersPage(userEntity);
    }

    @Override
    public void add(UserEntity userEntity) {
        if (!ObjectUtils.allNotNull(userEntity.getUserName(), userEntity.getPassword(), userEntity.getCompanyId(), userEntity.getCompanyName())) {
            throw new ShopException("用户信息不完整");
        }
        /*if (user.getUserName().trim().length() < 5) {
            throw new ShopException("用户名长度小于5");
        }
        if (user.getPassword().trim().length() < 5) {
            throw new ShopException("用户密码长度小于5");
        }*/
        if (!Objects.equals(userEntity.getUserName(),"admin")) {
            CompanyEntity companyEntity = companyMapper.selectById(userEntity.getCompanyId());
            if (Objects.isNull(companyEntity)) {
                throw new ShopException("公司不存在");
            }
        }
        UserEntity dbUserEntity = userRepository.selectByName(userEntity.getUserName());
        if (Objects.nonNull(dbUserEntity)) {
            throw new ShopException("用户已存在");
        }

        userMapper.insert(userEntity);
    }

    @Override
    public UserEntity loginCheck(String userName, String password) {
        UserEntity userEntity = userRepository.selectByName(userName);
        if (Objects.nonNull(userEntity) && userEntity.getPassword().equals(MD5Utils.encryptByMd5(password))){
            if (!userEntity.getCompanyName().equals("admin")) {
                CompanyEntity companyEntity = companyMapper.selectById(userEntity.getCompanyId());
                if (Objects.nonNull(companyEntity) && companyEntity.isNormal()) {
                    return userEntity;
                }
                return null;
            }
            return userEntity;
        }
        return null;
    }

    @Override
    public void initPassword(String userName) {
        UserEntity userEntity = userRepository.selectByName(userName);
        if (Objects.nonNull(userEntity)) {
            userEntity.setPassword(MD5Utils.encryptByMd5(SysConstant.INIT_PASSWORD));
            userMapper.updateById(userEntity);
            return;
        }
        throw new ShopException("用户不存在");
    }

    @Override
    public void updatePassword(String newPassword) {
        UserEntity userEntity = ThreadLocalUtil.get();
        userEntity.setPassword(MD5Utils.encryptByMd5(newPassword));
        userMapper.updateById(userEntity);
    }

    @Override
    public void updateCompanyName(Integer companyId, String companyName) {
        UpdateWrapper<UserEntity> wrapper = Wrappers.update();
        wrapper.set("company_name", companyName);
        wrapper.eq("company_id", companyId);
        userMapper.update(null,wrapper);
    }
}
